<template>
  <div class="main-container">
    <aside class="sidebar" :class="{ 'collapsed': isCollapsed }">
      <div class="menu">
        <!-- 添加标题 -->
        <div class="menu-title">
          <img src="/favicon.ico" alt="logo" class="title-icon" />
          <span class="text" v-show="!isCollapsed">智文助手</span>
        </div>
        <!-- 红色框内容 -->
        <button class="menu-item">
          <span class="icon">🔍</span>
          <span class="text" v-show="!isCollapsed">AI 搜索</span>
        </button>
        <button class="menu-item">
          <span class="icon">✍️</span>
          <span class="text" v-show="!isCollapsed">帮我写作</span>
        </button>
        <button class="menu-item">
          <span class="icon">🕒</span>
          <span class="text" v-show="!isCollapsed">最近对话</span>
        </button>
        <button class="menu-item">
          <span class="icon">📁</span>
          <span class="text" v-show="!isCollapsed">我的文档</span>
        </button>
      </div>
      <!-- <div class="recent-conversations" v-show="!isCollapsed"> -->
      <!-- 绿色框内容 -->
      <!-- <h3>历史对话</h3>
        <button class="recent-item">自媒体文章创作</button>
        <button class="recent-item">写平台名称文章</button>
        <button class="recent-item">论文模块分类与提示</button>
        <button class="recent-item">撰写英语学习文章</button>
        <button class="recent-item">手机版对话</button>
        <button class="recent-item">查看全部</button>
      </div> -->
    </aside>
    <!-- 切换按钮 -->
    <button class="toggle-button" @click="toggleSidebar">
      {{ isCollapsed ? '›' : '‹' }}
    </button>
    <main class="content" :class="{ 'expanded': isCollapsed }">
      <!-- 主内容区域 -->
      <div class="chat-container">
        <h1 class="greeting">{{ greetingMessage }}，{{ state.username }}</h1>
        <p class="sub-greeting">你的超级助手已上线，为你带来高效服务 🚀</p>

        <!-- 聊天输入框 -->
        <div class="chat-input-container" @click="navigateTo('ai-tools')">
          <div class="chat-input">
            <span class="placeholder">来和我聊天吧！</span>
          </div>
        </div>

        <!-- 功能按钮区域 -->
        <div class="function-buttons">
          <button v-if="canAccessChat" @click="navigateTo('ai-tools')" class="function-button">

            <span>AI写作</span>
          </button>
          <button v-if="canAccessWriting" @click="navigateTo('ai-writing')" class="function-button">

            <span>帮我写作</span>
          </button>
          <button v-if="canAccessRecent" @click="navigateTo('recent-conversations')" class="function-button">

            <span>最近聊天</span>
          </button>
          <button v-if="canAccessDocuments" @click="navigateTo('my-documents')" class="function-button">

            <span>我的文档</span>
          </button>
        </div>
      </div>
    </main>
  </div>
</template>

<script setup>
import { computed, reactive, ref, onMounted, onBeforeUnmount } from 'vue';
import useUserStore from '@/store/modules/user';
import { useRouter } from 'vue-router';

const router = useRouter();
const userStore = useUserStore();
const isCollapsed = ref(false);

const state = reactive({
  username: userStore.name,
  showMainContent: true,
});

const roles = computed(() => userStore.roles || []);
const isAdmin = computed(() => roles.value.includes('admin'));

// 判断是否有权限访问相应的功能
const canAccessChat = computed(() =>
  roles.value.includes('common') ||
  roles.value.includes('vip') ||
  roles.value.includes('svip')
);

const canAccessWriting = computed(() =>
  roles.value.includes('svip') || roles.value.includes('vip')
);

const canAccessRecent = computed(() =>
  roles.value.includes('common') ||
  roles.value.includes('vip') ||
  roles.value.includes('svip')
);

const canAccessDocuments = computed(() =>
  roles.value.includes('svip') || roles.value.includes('vip')
);
const greetingMessage = computed(() => {
  const hour = new Date().getHours();
  if (hour < 12) {
    return '早上好';
  } else if (hour < 18) {
    return '中午好';
  } else {
    return '晚上好';
  }
});
// Carousel logic
const currentSlide = ref(0);

// 替换为你提供的图片 URL
const carouselItems = ref([
  { src: '/images/vip1.jpg', alt: 'Slide 1' },
  { src: '/images/recharge1.jpg', alt: 'Slide 2' },
]);

const nextSlide = () => {
  currentSlide.value = (currentSlide.value + 1) % carouselItems.value.length;
};

const prevSlide = () => {
  currentSlide.value = (currentSlide.value - 1 + carouselItems.value.length) % carouselItems.value.length;
};

const getCarouselItemStyle = (index) => {
  return {
    transform: `translateX(-${currentSlide.value * 100}%)`,
    transition: 'transform 0.5s ease',
  };
};

// 添加悬浮球点击逻辑
const goToSlide = (index) => {
  currentSlide.value = index;
};

// 根据当前轮播图的索引跳转到不同页面
const goToPage = (index) => {
  if (index === 0) {
    router.push('/front/vip'); // 点击图片一跳转到 VIP 页面
  } else if (index === 1) {
    router.push('/front/recharge'); // 点击图片二跳转到 Recharge 页面
  }
};

// 自动切换轮播图
let carouselInterval;
onMounted(() => {
  // 设置定时器，每3秒自动切换轮播图
  carouselInterval = setInterval(nextSlide, 3000);
});

onBeforeUnmount(() => {
  // 清除定时器，避免内存泄漏
  clearInterval(carouselInterval);
});

function navigateTo(page) {
  const fullPath = `/front/${page}`;
  router.push(fullPath);
}

function toggleSidebar() {
  isCollapsed.value = !isCollapsed.value;
}

</script>

<style scoped>
.main-container {
  display: flex;
  height: 100vh;
  background-color: #f7f9fc;
}

.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 200px;
  background-color: #fff;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  transition: all 0.3s ease;
  z-index: 1000;
  border-right: 1px solid #eaeaea;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
}

.sidebar.collapsed {
  width: 60px;
  padding: 16px 0;
}

.toggle-button {
  position: fixed;
  left: 200px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 50px;
  background-color: #fff;
  border: 1px solid #eaeaea;
  border-left: none;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  z-index: 1001;
  transition: all 0.3s ease;
  color: #666;
  font-size: 12px;
  box-shadow: 2px 0 5px rgba(0, 0, 0, 0.05);
}

.sidebar.collapsed+.toggle-button {
  left: 60px;
}

.menu {
  display: flex;
  flex-direction: column;
  padding: 0 16px;
}

.menu-title {
  display: flex;
  align-items: center;
  padding: 16px 8px;
  margin-bottom: 16px;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  border-bottom: 1px solid #eaeaea;
}

.title-icon {
  width: 20px;
  height: 20px;
  min-width: 20px;
  margin-right: 12px;
  object-fit: contain;
}

.menu-title .text {
  transition: opacity 0.3s ease;
  flex: 1;
}

.sidebar.collapsed .menu-title {
  padding: 16px 0;
  justify-content: center;
}

.sidebar.collapsed .menu-title .title-icon {
  margin-right: 0;
}

.menu-item {
  display: flex;
  align-items: center;
  background: none;
  border: none;
  padding: 10px 8px;
  text-align: left;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 4px;
  white-space: nowrap;
  margin-bottom: 4px;
  width: 100%;
  overflow: hidden;
}

.menu-item .icon {
  width: 20px;
  min-width: 20px;
  margin-right: 12px;
  font-size: 16px;
  text-align: center;
}

.menu-item .text {
  transition: opacity 0.3s ease;
  flex: 1;
}

.sidebar.collapsed .menu-item {
  padding: 10px 0;
  justify-content: center;
}

.sidebar.collapsed .menu-item .icon {
  margin-right: 0;
}

.sidebar.collapsed .menu {
  padding: 0 8px;
}

.menu-item:hover {
  background-color: #f5f5f5;
  transform: translateX(4px);
}

.recent-conversations {
  margin-top: 24px;
  padding: 0 16px;
}

.recent-conversations h3 {
  font-size: 13px;
  margin-bottom: 12px;
  color: #999;
  padding-left: 8px;
}

.recent-item {
  background: none;
  border: none;
  padding: 8px;
  text-align: left;
  font-size: 13px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 4px;
  width: 100%;
  margin-bottom: 2px;
}

.recent-item:hover {
  background-color: #f5f5f5;
  transform: translateX(4px);
}

/* 新增主内容区域样式 */
.content {
  flex: 1;
  margin-left: 200px;
  padding: 40px;
  overflow-y: auto;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-color: #f7f9fc;
}

.content.expanded {
  margin-left: 60px;
}

.chat-container {
  width: 100%;
  max-width: 800px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.greeting {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 10px;
  text-align: center;
}

.sub-greeting {
  font-size: 16px;
  color: #666;
  margin-bottom: 40px;
  text-align: center;
}

.chat-input-container {
  width: 100%;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  background-color: #fff;
  overflow: hidden;
  margin-bottom: 40px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.chat-input-container:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
  transform: translateY(-2px);
}

.chat-input {
  padding: 16px 20px;
  font-size: 15px;
}

.placeholder {
  color: #999;
}

.function-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
  width: 100%;
  max-width: 700px;
}

.function-button {
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #fff;
  border: 1px solid #e0e0e0;
  border-radius: 24px;
  padding: 10px 20px;
  font-size: 14px;
  color: #333;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.function-button:hover {
  background-color: #f5f5f5;
  transform: translateY(-3px);
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.function-icon {
  margin-right: 8px;
  font-size: 16px;
}

/* 添加滚动条样式 */
.sidebar::-webkit-scrollbar,
.content::-webkit-scrollbar {
  width: 4px;
}

.sidebar::-webkit-scrollbar-thumb,
.content::-webkit-scrollbar-thumb {
  background-color: #ddd;
  border-radius: 2px;
}

.sidebar::-webkit-scrollbar-track,
.content::-webkit-scrollbar-track {
  background-color: transparent;
}
</style>